#!/usr/bin/env python
# coding=utf-8
# __author__ = 'Yunchao Ling'

# import os

# for line in os.popen("ps xa"):
# line=line.rstrip()
# print line

import paramiko
import sys
import re
import ConfigParser

config = ConfigParser.ConfigParser()
config.read("config.ini")

host = config.get("node", "host")
user = config.get("node", "user")
node = config.get("node", "node")
regex = config.get("regex", "regex")

ssh = paramiko.SSHClient()
ssh.load_system_host_keys()
ssh.connect(host)
filelist=[]
stdin, stdout, stderr = ssh.exec_command("ls")
for line in stdout:
    line = line.rstrip()
    match = re.match(regex, line)
    if match:
        filelist.append(line)
if len(filelist)!=0:
    cmd="mv "+filelist[0]+" "+filelist[0]+".node"+node
    stdin,stdout,stderr=ssh.exec_command(cmd)

ssh.close()
